<template>
  <div class="signAward">
    <van-overlay
      :show="show"
      :z-index=100
    />
    <van-loading v-if="show" color="#1989fa" size="50px"/>
    <div class="shadeBox"
         v-if="GoSignAwardFlag ">
      <div class="shade"></div>
      <div class="shadeCon">
        <img src="../../../static/index/sign-12.png">
        <img src="../../../static/index/sign-13.png"
             @click.one="GoSignAward">
        <p>签到成功，获得 <span>{{sign_score}}</span> 积分，已进入账户</p>
      </div>
    </div>
    <van-nav-bar title="签到领积分"
                 left-text=""
                 left-arrow
                 @click-left="onClickLeft"/>
    <div class="signAwardInfo">
      <p>{{userIntegral}}<span>积分</span></p>
      <p v-if="userIntegral > 0">当前积分共可兑换 {{flitersNum}} 枚金币</p>
      <p @click="GoSignAward" v-if="GoSignAwardBtn">立即签到</p>
      <span class="infoBtnCorC" v-else>已签到</span>
    </div>
    <div class="exchange">
      <div class="exchangeTop">
        <p>积分兑换金币</p>
        <p>兑换 <input type="number"
                     value=""
                     v-model="exchangeNum">枚金币 </p>
        <p>*注:兑换的积分请输入10的倍数</p>
        <p @click="GoexChange">立即兑换</p>
      </div>
      <div class="exchangeCon">
        <div class="exchangeMsg">
          <span>会员积分策略相关说明</span>
        </div>
        <ul>
          <li>
            <span>操作事件</span>
            <span>积分变化</span>
          </li>
          <li>
            <span>注册成功</span>
            <span>+10</span>
          </li>
          <li>
            <span>登录签到</span>
            <span>+2</span>
          </li>
          <li>
            <span>发布分类信息</span>
            <span>+2</span>
          </li>
          <li>
            <span>营业执照认证</span>
            <span>+10</span>
          </li>
          <li>
            <span>身份证认证</span>
            <span>+10</span>
          </li>
          <li>
            <span>注册推广</span>
            <span>一个会员增加积分5</span>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
  import {getStore, setStore} from '@/config/mUtils'

  export default {
    name: "signIntegral",
    data() {
      return {
        userIntegral: 0, //积分
        goldNum: 0, //金币
        GoSignAwardFlag: false, //立即签到弹窗
        GoSignAwardBtn: true,
        exchangeNum: '', //兑换金币
        sign_score: getStore("sign_score"), //获取默认全局配置信息
        show: true,
        str: {}
      }
    },
    computed: {
      //数字的小数部分去掉，只保留整数部分
      flitersNum() {
        return this.goldNum = Math.trunc(this.userIntegral / Number(getStore("score_fee")))
      }
    },
    methods: {
      //个人积分
      initDate() {
        this.$axios.post('user/info', this.qs.stringify({
          "token": getStore('token')
        })).then((res) => {
          this.userIntegral = res.data.data.score
          this.show = false
          // this.goldNum = Math.trunc(this.userIntegral / Number(this.UserInfoPage.score_fee))
        });

        //签到检查
        this.$axios.post('user/checkSign', this.qs.stringify({
          "token": getStore('token')
        })).then((res) => {
          if ( res.data.status === 0 ) {
            this.GoSignAwardBtn = false
          }
        });
      },
      //立即签到
      GoSignAward() {

        this.$axios.post('user/signin', this.qs.stringify({
          "token": getStore("token"),
        })).then((res) => {
          this.initDate()
          this.GoSignAwardFlag = !this.GoSignAwardFlag
        })
      },
      //立即兑换
      GoexChange() {
        if ( this.exchangeNum !== '' ) {
          if ( this.exchangeNum > this.goldNum ) {
            this.$toast.fail('积分不足')
            // this.exchangeNum = ''
            return
          } else {
            this.$dialog.confirm({
              title: '支付积分',
              message: `确定使用${this.exchangeNum * getStore("credit_fee")} 积分兑换${this.exchangeNum}金币吗？`
            }).then(() => {
              this.$axios.post('user/addcoins', this.qs.stringify({
                "token": getStore("token"),
                "score": this.exchangeNum * getStore("score_fee")
              })).then((res) => {

                this.$toast.success(res.data.message)
                this.exchangeNum = ''
                this.initDate()
              })
            }).catch(() => {
              this.$toast.fail('取消兑换')
            });
          }
        } else {
          this.$toast('请输入兑换数量')
          return false
        }
      },
      onClickLeft() {
        this.$router.go(-1)
      }
    },
    created() {
      this.initDate()
    }
  }
</script>

<style scoped lang="scss">
  @import "../../../static/css/user/user";

  .shade {
    z-index: 88 !important;
  }

  .shadeCon {
    z-index: 99 !important;
  }

  .signAward .exchange input {
    border: 2px solid #a0a0a0;
  }
</style>
